<template>
  <!-- 版心 -->
  <el-container class="main-container">
    <!-- 1.左侧边栏 -->
    <el-aside width="200px">
      <!-- 1.1 个人信息盒子 -->
      <div class="user-box">
        <!-- v-if v-else : 有头像显示头像,没有显示默认头像 -->
        <img :src="user.user_pic" alt="" v-if="user.user_pic" width="30px"/>
        <img src="@/assets/logo.png" alt="" v-else  width="30px"/>
        <span>欢迎 {{ user.nickname || user.username }}</span>
      </div>
      <!-- 1.2 菜单栏 -->
      <el-menu
        :default-active="$route.path"
        class="el-menu-vertical-demo"
        background-color="#23262e"
        text-color="#fff"
        active-text-color="#f08"
        unique-opened
        router
      >
        <template v-for="item in menus">
          <!-- (1)el-menu-item: 没有子标题 -->
          <el-menu-item
            :index="item.indexPath"
            v-if="!item.children"
            :key="item.indexPath"
          >
            <i :class="item.icon"></i>
            <span slot="title">{{ item.title }}</span>
          </el-menu-item>
          <!-- (2)el-submenu : 有子标题 -->
          <el-submenu :index="item.indexPath" v-else :key="item.indexPath">
            <!-- 具名插槽显示标题头部 -->
            <template #title>
              <i :class="item.icon"></i>
              <span>{{ item.title }}</span>
            </template>
            <el-menu-item
              v-for="ele in item.children"
              :key="ele.indexPath"
              :index="ele.indexPath"
            >
              <i :class="ele.icon"></i>
              {{ ele.title }}
            </el-menu-item>
          </el-submenu>
        </template>
      </el-menu>
    </el-aside>
    <!-- 2.右侧内容 -->
    <el-container>
      <!-- 2.1 右侧顶部 -->
      <el-header>
        <!-- (1)左边:logo盒子 -->
        <div class="logo"></div>
        <!-- (2)右边:横向menu -->
        <el-menu
          mode="horizontal"
          class="el-menu-vertical-demo"
          unique-opened
          router
        >
          <!-- (1)el-submenu : 有子标题 -->
          <el-submenu index="1">
            <!-- 具名插槽显示标题头部 -->
            <template #title>
              <!-- v-if v-else : 有头像显示头像,没有显示默认头像 -->
              <img :src="user.user_pic" alt="" v-if="user.user_pic" class="avatar"/>
              <img src="@/assets/logo.png" class="avatar" v-else />

              <span>个人中心</span>
            </template>
            <el-menu-item index="/user-info">
              <i class="el-icon-s-operation"></i>
              基本资料
            </el-menu-item>
            <el-menu-item index="/user-avatar">
              <i class="el-icon-camera"></i>
              更换头像
            </el-menu-item>
            <el-menu-item index="/user-pwd">
              <i class="el-icon-key"></i>
              重置密码
            </el-menu-item>
          </el-submenu>
          <!-- (2)el-menu-item: 没有子标题 -->
          <el-menu-item>
            <i class="el-icon-switch-button"></i>
            <span slot="title" @click="logout">退出</span>
          </el-menu-item>
        </el-menu>
      </el-header>
      <!-- 2.2 右侧主题main -->
      <el-main>
        <!-- 二级路由出口 -->
        <router-view></router-view>
      </el-main>
      <!-- 2.3 右侧底部 -->
      <el-footer>© www.itheima.com - 黑马程序员</el-footer>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Main",
  data() {
    return {
      //菜单栏数组
      menus: []
    };
  },
  //computed:计算属性简写vuex数据
  computed: {
    user() {
      return this.$store.state.user;
    }
  },
  methods: {
    //点击退出登录
    async logout(){
      //(1)弹窗提示用户
      const confirm = await this.$confirm('确认退出登录吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).catch(()=>{})

      //(2)点击确认退出登录
      if( confirm ){
        //清除token
        this.$store.commit('updateToken','')
        //跳转登录页
        this.$router.push('/login')
      }
    }
  },
  //created:页面一加载
  async created() {
    //(1)发送ajax请求个人信息 : 提交actions
    this.$store.dispatch("getUserInfo");
    //(2)发送ajax请求左侧菜单栏
    const { data: res } = await this.$axios.get("/my/menus");
    //挂载到data
    this.menus = res.data;
  }
};
</script>

<style lang="less" scoped>
.main-container {
  height: 100%;
  .el-aside {
    background-color: #23262e;
    .el-menu-vertical-demo {
      background-color: #23262e;
    }
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #f2f2f2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}

//右侧样式
.el-header {
  .logo {
    height: 45px;
    width: 150px;
    padding: 8px 0px;
    user-select: none;
    text-align: center;
    background: url("@/assets/images/logo.png") center center / 100px no-repeat
      rgb(85, 85, 85);
    border-radius: 3px;
    margin-left: 15px;
  }
}

// 左侧边栏用户信息区域
.user-box {
  height: 70px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-top: 1px solid #000;
  border-bottom: 1px solid #000;
  user-select: none;
  img {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    background-color: #fff;
    margin-right: 15px;
    object-fit: cover;
  }
  span {
    color: white;
    font-size: 12px;
  }
}

// 侧边栏菜单的样式
.el-aside {
  .el-submenu,
  .el-menu-item {
    width: 200px;
    user-select: none;
  }
}
</style>
